package functionHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import dataObject.Brand;
import dataObject.Product;
import dataView.Rank;
import exception.UserSelectionOutOfRangeException;

public class ScoreCalculator {
	ArrayList<Rank> allProduct;
	
	public ScoreCalculator(ArrayList<Rank> allProduct)
	{
		this.allProduct = allProduct;
	}
	//////////////////////////////////////////Calculate score.
	public void calPriorityScores(String userInput, ArrayList<Brand> brandList) throws UserSelectionOutOfRangeException
	{
		String[] priority = userInput.split(",");
		int counter = 0;
		int score = 20;
		priority = removeRedundancy(priority);
		if (priority[0].equals("all") || priority[0].equals("ALL") || priority[0].equals("All")){
			for(Rank r:allProduct)
			{
				r.setQualify(true);
				r.addScore(score);
			}
		}
		else {
			for(String p:priority){
				if (Integer.parseInt(p)>brandList.size()||Integer.parseInt(p)<1)
				{
					throw new UserSelectionOutOfRangeException();
				}
				for(Brand b:brandList)
				{
					counter++;
					if (Integer.parseInt(p)==counter)
					{
						for(Product a:b.getProductList())
						{
							for(Rank r:allProduct)
							{
								if(a.getName().equals(r.getName()))
								{
									r.setQualify(true);
									r.addScore(score);
								}
							}
						}
						break;
					}
					else
					{
						for(Product a:b.getProductList())
						{	
							for(Rank r:allProduct)
							{
								if(a.getName().equals(r.getName()))
								{
									if (r.getQualify()!=true)
										r.setQualify(false);
								}
							}
						}
					}
				}
				counter = 0;
				score -= 3;
				if(score<=0)
					score = 0;
			}
		}

	}
	
	public String[] removeRedundancy(String[] priority)
	{
		ArrayList<String> priorityTemp = new ArrayList<String>();
		
		for(int i=0;i<priority.length;i++){
			String current = priority[i];
			int count = 0;
			for (String temp : priorityTemp){
				if (temp.equals(current)) count += 1;
			}
			if (count == 0) priorityTemp.add(current);
		}
		return priorityTemp.toArray(new String[priorityTemp.size()]);
	}
	//////////////////////////////remove unqualified product
	public void qualifiedProductByBudget(String userInput)
	{
		String[] budget = userInput.split("-");
		if(budget.length==1)
		{
			for(Rank r:allProduct)
			{
				if(r.getFinalPrice() > Integer.parseInt(budget[0]))
				{
					r.setQualify(false);
				}
			}
		}
		else
		{
			for(Rank r:allProduct)
			{
				if(r.getFinalPrice() < Integer.parseInt(budget[0]) || r.getFinalPrice() > Integer.parseInt(budget[1]) )
				{
					r.setQualify(false);
				}
			}
		}
	}
	
	public void qualifiedProductByMonSize(String userInput)
	{
		String [] monsize = userInput.split("-");
		if(monsize.length==1)
		{
			for(Rank r:allProduct)
			{	
				if(r.getSize() > Double.parseDouble(monsize[0]))
				{
					r.setQualify(false);
				}
			}
		}
		else
		{
			for(Rank r:allProduct)
			{	
				if(r.getSize() < Double.parseDouble(monsize[0]) || r.getSize() >  Double.parseDouble(monsize[1]) )
				{
					r.setQualify(false);
				}
			}
		}
	}
	
	public void calBatterySizeScores()
	{
		int score = 20;

		Collections.sort(allProduct, new Comparator<Rank>()
				{
			@Override
			public int compare(Rank o1, Rank o2) {
				return (o2.getBatterySize())-(o1.getBatterySize());
			}

				});
		ArrayList<Integer> batteryList = new ArrayList<Integer>();
		int preBattery = 0;
		for(Rank r:allProduct)
		{
			if(r.getBatterySize()!=preBattery)
			{
				preBattery = r.getBatterySize();
				batteryList.add(r.getBatterySize());
			}
		}
		int batteryCounter = 0;
		for(Rank r:allProduct)
		{
			if(batteryList.get(batteryCounter)!=r.getBatterySize())
			{
				batteryCounter++;
				score -= 3;
				if(score<=0)
					score = 0;
			}
			r.addScore(score);

		}
	}
	
	public void calAvailableColor()
	{
		int score = 20;

		boolean newColor = false;

		Collections.sort(allProduct, new Comparator<Rank>()
				{
			@Override
			public int compare(Rank o1, Rank o2) {
				return (o2.getAvailableColor())-(o1.getAvailableColor());
			}

				});
		ArrayList<Integer> colorList = new ArrayList<Integer>();
		int preColor = 0;
		for(Rank r:allProduct)
		{
			if(r.getAvailableColor()!=preColor)
			{
				preColor = r.getAvailableColor();
				colorList.add(r.getAvailableColor());
			}
		}
		
		int colorCounter = 0;
		for(Rank r:allProduct)
		{
			if(colorList.get(colorCounter)!=r.getAvailableColor())
			{
				colorCounter++;
				score -= 3;
				if(score<=0)
					score = 0;
			}
			r.addScore(score);
		}
	}
	
	public void calCameraPixelScores()
	{	
		int score = 20;

		boolean newPixel = false;

		Collections.sort(allProduct, new Comparator<Rank>()
				{
			@Override
			public int compare(Rank o1, Rank o2) {
				return (o2.getCameraPixel())-(o1.getCameraPixel());
			}

				});
		ArrayList<Integer> pixelList = new ArrayList<Integer>();
		int prePixel = 0;
		for(Rank r:allProduct)
		{
			if(r.getCameraPixel()!=prePixel)
			{
				prePixel = r.getCameraPixel();
				pixelList.add(r.getCameraPixel());
			}
		}
		int pixelCounter = 0;
		for(Rank r:allProduct)
		{
			if(pixelList.get(pixelCounter)!=r.getCameraPixel())
			{
				pixelCounter++;
				score -= 3;
				if(score<=0)
					score = 0;
			}
			r.addScore(score);
		}

	}
}


